
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>产品销量排行</title>
	<link href="../../rs/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
	<link href="../../rs/css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
	<link href="../../rs/plugins/bootstrap_tab/css/bootstrap-table.css" rel="stylesheet">
	<link href="../../rs/css/animate.min.css" rel="stylesheet">
	<link href="../../rs/css/style.min862f.css?v=4.1.0" rel="stylesheet">
	<link href="../../rs/plugins/datapicker/datepicker3.css" rel="stylesheet">
  <link rel="stylesheet" href="//rawgit.com/vitalets/x-editable/master/dist/bootstrap3-editable/css/bootstrap-editable.css">

  <style>
         #toolbar1 {
             padding: 10px 15px;
             float: right;
         }
         .btn-group-sm > .btn, .btn-sm {
             padding: 5px 12px;
         }

         .editable-click, a.editable-click, a.editable-click:hover {
             border-bottom: 0px;
         }
         .fixed-table-pagination .page-list{
             display: none;
         }
     </style>



<?php
  require_once('login.php');
 ?>


</head>

<body class="gray-bg">
  <div class="wrapper wrapper-content animated fadeInRight">
      <!-- Panel Other -->
      <div class="ibox float-e-margins">
          <div class="ibox-content">
              <div class="row">
                  <div id="toolbar1">
                      <button onclick="cancelall()" style="float:left;margin-left:5px" class="btn" id="del">
                          <i class="glyphicon glyphicon-remove"></i> 删除
                      </button>
                      <button onclick="save_do()" style="float:left;margin-left:5px" class="btn btn-success">
                          <i class="glyphicon glyphicon-ok"></i> 保存
                      </button>
                      <button onclick="save_sh()" style="float:left;margin-left:5px" class="btn btn-danger">
                          <i class="glyphicon glyphicon-zoom-in"></i> 审核
                      </button>
                  </div>
              </div>
              <div class="row">
                  <div class="col-sm-4 m-b-xs">
                      <div class="form-group" id="data_1">
                          <div class="input-group date">
                              <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                              <input type="text" class="form-control" placeholder="请选择单据日期" name="start" id="start" readonly="readonly">
                          </div>
                      </div>

                  </div>
                  <div class="col-sm-4 m-b-xs">
                      <div class="form-group draggable">
                          <div class="input-group">
                                  <span class="input-group-btn">
                                      <button style="height:34px;background-color: #FFF;border: 1px solid #e5e6e7;font-size: 14px" type="button" class="btn btn-sm ">订 单</button>
                                  </span>
                              <select class="form-control" id="selectorder">
                                  <option value="-1">新单</option>
                              </select>
                          </div>
                      </div>
                  </div>
                  <div class="col-sm-4 m-b-xs" style="float: right;">
                      <div class="input-group">
                          <input type="text" class="form-control" id="searchtxt" onkeydown="_key()">
                          <div class="input-group-btn">
                              <button type="button" class="btn btn-white dropdown-toggle" onclick="open_modal()">
                                  搜索
                              </button>
                              <ul class="dropdown-menu dropdown-menu-right" role="menu"></ul>
                          </div>
                      </div>
                  </div>
              </div>
              <div class="row">
                  <div>
                      <button style="float:left;margin-left: 15px; margin-right:5px;margin-bottom: 20px" id="remove" class="btn btn-danger" disabled>
                          <i class="glyphicon glyphicon-trash"></i>
                      </button>
                      <div class="col-sm-3">
                          <div class="input-group ">
                                      <span class="input-group-btn">
                                          <button style="height:34px;background-color: #FFF;border: 1px solid #e5e6e7;font-size: 14px" type="button" class="btn btn-sm ">出库店</button>
                                      </span>
                              <select class="form-control" id="mdbh">
                              </select>
                          </div>
                      </div>
                      <div class="col-sm-3">
                          <div class="input-group ">
                                      <span class="input-group-btn">
                                          <button style="height:34px;background-color: #FFF;border: 1px solid #e5e6e7;font-size: 14px" type="button" class="btn btn-sm ">入库店</button>
                                      </span>
                              <select class="form-control" id="mdbh1">
                              </select>
                          </div>
                      </div>
                      <div class="col-sm-5">
                          <div class="input-group">
                              <div class="input-group-btn">
                                  <button type="button" class="btn btn-white dropdown-toggle " data-toggle="dropdown">
                                      订单备注
                                  </button>
                              </div>
                              <input type="text" class="form-control" id="djbz">
                          </div>
                      </div>
                  </div>
              </div>
              <table id="table"></table>
              <!-- End Example Card View -->
          </div>
      </div>
  </div>






<!--
  <script src="js/bootstrap-suggest.min.js"></script> -->






<script src="../../rs/js/jquery.min.js"></script>
<script src="../../rs/js/bootstrap.js"></script>
<script src="../../rs/plugins/bootstrap_tab/js/bootstrap-table.min.js"></script>
<script src="../../rs/plugins/bootstrap_tab/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<script src="../../rs/plugins/datapicker/bootstrap-datepicker.js"></script>
<script src="../../rs/js/jquery.cookie.js"></script>
<script src="../../rs/js/common_qj.js"></script>
<script src="../../rs/plugins/layer/layer.min.js"></script>

 <script src="//rawgit.com/vitalets/x-editable/master/dist/bootstrap3-editable/js/bootstrap-editable.js"></script>

 <script type="text/javascript">
     var $table = $('#table');
     var $remove = $('#remove');
     var sign = URLencode($.cookie('sign'));

     $(function () {

         //日期选择插件
         $("#data_1 .input-group.date").datepicker({
             todayBtn: "linked",
             keyboardNavigation: !1,
             forceParse: !1,
             calendarWeeks: !0,
             autoclose: !0
         });

         var start = (new Date()).format('yyyy-MM-dd');
         $('#start').val(start);
         $('#table').bootstrapTable({
             columns: [
                 {
                     field: 'state',
                     checkbox: true
                 },
                 {
                     field: 'id',
                     align:'center',
                     width:'30px',
                     title: '序号'
                 },
                 {
                     field: 'prid',
                     title: '商品编号',
                     align: 'left',
                     width:"100px"
                 },
                 {
                     field: 'prname',
                     align: 'left',
                     title: '商品名称'
                 },
                 {
                     field: 'sj',
                     title: '单价',
                     width:'60px'

                 },
                 {
                     field: 'sl',
                     title: '数量',
                     editable: true,
                     width:'60px'
                 },
                 {
                     field: 'bz',
                     title: '备注',
                     editable: true
                 }
             ],
             advancedSearch: true,
             idTable: "advancedTable",
             shadeClose: true,
             toggle: "table",
             height: "299",
             //当修改数值后执行的保存操作
             onEditableSave: function (field, row, oldValue, $el) {
                 if (field == 'sl') {
                     if (isNaN(row.sl)) {
                         row.sl = oldValue;
                     }else {
                         row.sl=parseFloat(row.sl).toFixed(2);
                     }
                 }
                 if (field == 'sj') {
                     if (isNaN(row.sj)) {
                         row.sj = oldValue;
                     }else {
                         row.sj=parseFloat(row.sj).toFixed(2);
                     }
                 }
                 $('#table').bootstrapTable('load', dtn);
                 banhao();
                 return true;
             }
         });
        //  f_sheard();

         <!-- 删除操作 -->
         $table.on('check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table', function () {
             $remove.prop('disabled', !$table.bootstrapTable('getSelections').length);
         });
         $remove.click(function () {
             layer.confirm('确定要删除这个商品吗？', {
                 title: [''],
                 btn: ['确定', '取消'] //按钮
             }, function (index) {
                 //发送当前删除的数据
                 var rids = $.map($table.bootstrapTable('getSelections'), function (row) {
                     return row.rid;
                 });
                 $.ajax({
                     type: "post",
                     url: G_url+'kcyd/del_d',
                     data: 'rids=' + rids + '&sign=' + sign,
                     dataType: "json",
                     success: function (aftdata1) {
                         layer.closeAll();
                         if (!aftdata1.ok) {
                             layer.msg(aftdata1.msg);
                             if (aftdata1.code == '1000') {
                                 $.cookie('sign', '', {expires: -1});
                                 $.cookie('username', '', { expires: -1 });
                                 window.location.href = 'login.html';
                                 return;
                             }
                         }

                         //删除选中行
                         var rowindexs = [];
                         $('input[name="btSelectItem"]:checked').each(function () {
                             rowindexs.push($(this).data('index'));
                         });
                         var ndtn = [];
                         for (i = 0; i < dtn.length; i++) {
                             if ($.inArray(i, rowindexs) < 0) ndtn.push(dtn[i]);
                         }
                         dtn = ndtn;

                         $('#table').bootstrapTable('load', dtn);
                         banhao();
                         $remove.prop('disabled', true);
                     },
                     error: function (XMLHttpRequest, textStatus, errorThrown) {
                         layer.msg('数据删除失败', {time: 1000});
                     }
                 });
                 layer.close(index);
             }, function (index) {
                 layer.close(index);
             });
         });
         <!-- 删除操作 -->

         //隐藏按钮
         var cz1 = $("#selectorder  option:selected").text();
         if(cz1=='新单'){
             $("#del").hide();
         }

     });

     var dtnheard = {};
     var js = {};
     var sypr = {};
     function f_sheard() {
         layer.load(2);
         $.ajax({
             type: "post",
             url:G_url+'kcyd/sgdd_h',
             data: 'sign=' + sign,
             dataType: "json",
             success: function (aftdata) {
                 layer.closeAll('loading');
                 if (!aftdata.ok) {
                     layer.msg(aftdata.msg);
                     if (aftdata.code == '1000') {
                         $.cookie('sign', '', {expires: -1});
                         $.cookie('username', '', { expires: -1 });
                         window.location.href = 'login.html';
                         return;
                     }
                 }
                 dtnheard = aftdata;
                 js.value = dtnheard.order;
                 sypr = dtnheard.sypr;

                 //给订单编号做循环
                 var ss = dtnheard.order;
                 var html = '';
                 var i=0;
                 $.each(ss, function (a, b) {//直接取出json的prolist做each循环.
                     html += "<option value='"+i+"'>" + b.djbh + "</option>";//拼接字符串.
                     i++;
                 });
                 $("#selectorder").append(html);

                 //给门店编号做循环
                 var ff = dtnheard.sysh;
                 var html1 = '';
                 $.each(ff, function (a, b) {//直接取出json的prolist做each循环.
                     html1 += "<option value='" + b.shbh + "'>" + b.shmc + "</option>";//拼接字符串.
                 });
                 $("#mdbh").append(html1);
                 $("#mdbh1").append(html1);
             },
             error: function (XMLHttpRequest, textStatus, errorThrown) {
             }
         });
     }


     //给button添加回车事件
     function _key() {
         if(event.keyCode ==13){
             open_modal();
             $("#searchtxt").blur();
         }
     }

     function open_modal() {
         var s = $('#searchtxt').val();

         //页面层
         layer.open({
             title: ['添加商品'],
             type: 1,
             offset: '0px',//top顶部偏移
             skin: 'layui-layer-rim', //加上边框
             area: ['700px', '600px'], //宽高
             shadeClose: true,
             content: '<div style="padding: 20px 50px 0 50px "><table id="table1"></table></div>'
         });

         $('#table1').bootstrapTable('destroy');//删除table表格
         $('#table1').bootstrapTable({//重新建立表格
             columns: [
                 {
                     field: 'prid',
                     title: '商品编号'
                 },
                 {
                     field: 'prname1',
                     title: '商品名称'
                 },
                 {
                     field: 'priceout',
                     title: '单价'
                 },
                 {
                     field: 'insert',
                     title: '添加',
                     align: 'center',
                     formatter: insertFormatter
                 }
             ],
             shadeClose: true,
             searchText: s,
             data: sypr,
             toggle: "table",
             height: "500",
             search: true
         });
         banhao();
     }

     function insert_do(r) {
         var lno = 0;
         var sfcz = false;
         for (var i = 0, l = dtn.length; i < l; i++) {
             if (dtn[i].prid == r.prid) {
                 lno = i;
                 sfcz = true;
                 break
             }
         }

         if (sfcz) {
             layer.open({
                 content: '数据存已在是否累加?',
                 title: [''],
                 btn: ['累加', '新增'], //按钮
                 yes: function (index) {
                     //累加
                     dtn[lno].sl++;
                     $table.bootstrapTable('load', dtn);
                     banhao();
                     layer.close(index);
                 },
                 btn2: function (index) {
                     //新增
                     var n = {};
                     n.prid = r.prid;
                     n.prname = r.prname1;
                     n.sj = r.priceout;
                     n.sl = 1;
                     n.rid= 0;
                     n.bz="";
                     dtn.push(n);
                     $table.bootstrapTable('load', dtn);
                     banhao();
                     layer.close(index);
                 },
                 cancle: function (index) {
                     layer.close(index);
                 }
             });
         } else {
             //新增
             var n = {};
             n.prid = r.prid;
             n.prname = r.prname1;
             n.sj = r.priceout;
             n.sl = 1;
             n.rid= 0;
             n.bz="";
             dtn.push(n);
             $table.bootstrapTable('load', dtn);
             banhao();
         }

     }

     //使用函数将调用的那一行的样式写出
     function insertFormatter(value, row) {
         return "<button type='button' class='btn btn-info' onclick='insert_do(" + JSON.stringify(row) + ")'>添加</button> ";
     }


     var kk=-1;
     $("#selectorder").change(function () {
         layer.confirm('确定要选择这个订单吗？', {
             title: [''],
             btn: ['确定', '取消'] //按钮
         }, function (index) {
             var cz = $("#selectorder  option:selected").text();
             var cv = $("#selectorder  option:selected").val();
             kk=cv;
             if(cz=='新单'){
                 $("#del").hide();
                 location.reload();
             }else {
                 $("#del").show();
             }
             f_detail(cz);
             layer.close(index);
         }, function () {
             $("#selectorder").val(kk);
         });
     });

     var dtn = [];
     var sign1=$.cookie('sign');
     function f_detail(djbh) {
         layer.msg('数据加载中...', {icon: 16, time: 200000, shade: [0.1, 'black']});
         $.ajax({
             type: "post",
             url: G_url+'kcyd/sgdd_d',
             data: 'djbh=' + djbh + '&sign=' + sign,
             dataType: "json",
             success: function (aftdata) {
                 layer.closeAll();
                 if (!aftdata.ok) {
                     layer.msg(aftdata.msg);
                     if (aftdata.code == '1000') {
                         $.cookie('sign', '', {expires: -1});
                         $.cookie('username', '', { expires: -1 });
                         window.location.href = 'login.html';
                         return;
                     }
                 }
                 dtn = aftdata.detail;

                 //数据写进djbz
                 $("#selectorder option:selected").each(function(){
                     var l=$(this).index();
                     if(l=="0"){
                         return false;
                     }else {
                         $("#djbz").val(aftdata.order.sjxx);
                         $("#start").val(aftdata.order.djrq);
                     }
                 });


                 $('#table').bootstrapTable('load', dtn);
                 $("#mdbh").val(aftdata.order.shop);
                 $("#mdbh1").val(aftdata.order.shop2);
                 banhao();
             },
             error: function (XMLHttpRequest, textStatus, errorThrown) {
             }
         });
     }

     function cancelall(){
         layer.confirm('确定删除这个订单吗？', {
             title: [''],
             btn: ['确定', '取消'] //按钮
         }, function (index) {
             layer.msg('数据删除中...', {icon: 16, time: 200000, shade: [0.1, 'black']});
             var djbh_del = $("#selectorder  option:selected").text();
             $.ajax({
                 type: "post",
                 url: G_url+'kcyd/del',
                 data: 'djbh=' + djbh_del + '&sign=' + sign,
                 dataType: "json",
                 success: function (aftdata1) {
                     layer.closeAll();
                     if (!aftdata1.ok) {
                         layer.msg(aftdata1.msg);
                         if (aftdata1.code == '1000') {
                             $.cookie('sign', '', {expires: -1});
                             $.cookie('username', '', { expires: -1 });
                             window.location.href = 'login.html';
                             return;
                         }
                     }else {
                         location.reload();
                     }
                 },
                 error: function (XMLHttpRequest, textStatus, errorThrown) {
                     layer.msg('数据删除失败', {time: 1000});
                 }
             });
             layer.close(index);
         }, function (index) {
             layer.close(index);
         });
     }

     var f_data={};
     function save_do(){
         var yy=$("#mdbh  option:selected").val();
         var xx=$("#mdbh1  option:selected").val();
         if(yy==xx){
             layer.msg("出库店和入库店不能相同");
         }else{
             layer.confirm('确定要保存吗？', {
                 title: [''],
                 btn: ['确定', '取消'] //按钮
             }, function (index) {
                 layer.msg('数据保存中...', {icon: 16, time: 200000, shade: [0.1, 'black']});
                 var rq=$('#start').val();
                 var djbh=$("#selectorder  option:selected").text();
                 var djbz=$('#djbz').val();
                 var data=[{
                     "djrq":rq,
                     "djbh":djbh,
                     "djbz":djbz,
                     "shop":yy,
                     "shop2":xx,
                     total:'0'
                 }];
                 var data1=$('#table').bootstrapTable('getData');
                 var save={};
                 save.order=data;
                 save.detail=data1;
                 save.sign=sign1;
                 $.ajax({
                     type: "post",
                     url: G_url+'kcyd/save',
                     data:JSON.stringify(save),
                     dataType: "json",
                     success: function(res) {
                         if (res.ok){
                             layer.msg(res.msg);
                             f_data=res.detail;

                             var ss = dtnheard.order;
                             if(djbh=="新单"){
                                 $("#selectorder").append( "<option value='"+ res.djbh+"' selected>" + res.djbh + "</option>");
                             }

                             $("#del").show();

                             $('#table').bootstrapTable('load', f_data);
                         } else {
                             layer.msg(res.msg);
                             if (res.code == '1000') {
                                 $.cookie('sign', '', {expires: -1});
                                 $.cookie('username', '', { expires: -1 });
                                 window.location.href = 'login.html';
                                 return;
                             }
                         }
                     },
                     error: function (XMLHttpRequest, textStatus, errorThrown) {
                         layer.msg('数据保存失败', {time: 1000});
                     }
                 });
                 layer.close(index);
             }, function (index) {
                 layer.close(index);
             });
         }
     }

     function sh(){
         var yy=$("#mdbh  option:selected").val();
         var xx=$("#mdbh1  option:selected").val();
         if(yy==xx){
             layer.msg("出库店和入库店不能相同");
         }else {
             var rq=$('#start').val();
             var djbh=$("#selectorder  option:selected").text();
             var djbz=$('#djbz').val();
             var data=[{
                 "djrq":rq,
                 "djbh":djbh,
                 "djbz":djbz,
                 "shop":yy,
                 "shop2":xx,
                 total:'0'
             }];
             var data1=$('#table').bootstrapTable('getData');
             var save={};
             save.order=data;
             save.detail=data1;
             save.sign=sign1;
             $.ajax({
             async: false,
             type: "post",
             url: G_url+'kcyd/save',
             data:JSON.stringify(save),
             dataType: "json",
             success: function(res) {
                 if (res.ok){
                     f_data=res.detail;

                     var ss = dtnheard.order;
                     if(djbh=="新单"){
                         $("#selectorder").append( "<option value='"+ res.djbh+"' selected>" + res.djbh + "</option>");
                     }
                     $("#del").show();
                     $('#table').bootstrapTable('load', f_data);
                 } else {
                     layer.msg(res.msg);
                     if (res.code == '1000') {
                         $.cookie('sign', '', {expires: -1});
                         $.cookie('username', '', { expires: -1 });
                         window.location.href = 'login.html';
                         return;
                     }
                 }
             },
             error: function (XMLHttpRequest, textStatus, errorThrown) {
                 layer.msg('数据保存失败', {time: 1000});
             }
         });
         }
     }

     function save_sh(){
         layer.confirm('确定提交审核吗？', {
             title: [''],
             btn: ['确定', '取消'] //按钮
         }, function (index) {
             sh();
             layer.msg('数据提交中...', {icon: 16, time: 200000, shade: [0.1, 'black']});
             var djbhsh=$("#selectorder  option:selected").text();
             $.ajax({
                 async: false,
                 type: "post",
                 url: G_url+'kcyd/sh',
                 data:'djbh=' + djbhsh + '&sign=' + sign,
                 dataType: "json",
                 success: function(res) {
                     if (res.ok){
                         layer.msg(res.msg);
                         location.reload();
                     } else {
                         layer.msg(res.msg);
                         if (res.code == '1000') {
                             $.cookie('sign', '', {expires: -1});
                             $.cookie('username', '', { expires: -1 });
                             window.location.href = 'login.html';
                             return;
                         }
                     }
                 },
                 error: function (XMLHttpRequest, textStatus, errorThrown) {
                     layer.msg('数据提交失败', {time: 1000});
                 }
             });
             layer.close(index);
         }, function (index) {
             layer.close(index);
         });
     }


 </script>
</body>
</html>
